package com.cw.common.api;


import lombok.Data;

import java.io.Serializable;

/**
 * @description:返回前段数据类型
 * @author: wzw
 * @time: 2019/11/4 15:30
 */

@Data
public class ResultObject implements Serializable {

    private static final long serialVersionUID = -8628409565343105238L;
    private long code;
    private String message;
    private Object data;


    public ResultObject() {
    }

    public ResultObject(Integer code) {
	  this.code = code;
    }

    public ResultObject(Integer code, String message) {
	  this.code = code;
	  this.message = message;
    }

    public ResultObject(Integer code, Object data) {
	  this.code = code;
	  this.data = data;
    }

    public ResultObject(Integer code, Object data, String message) {
	  this.code = code;
	  this.data = data;
	  this.message = message;
    }



    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> ResultObject success(T data) {
	  return new ResultObject(ResultCode.SUCCESS.getCode(), data,
					     ResultCode.SUCCESS.getMessage());
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    public static <T> ResultObject success(Object data, String message) {
	  return new ResultObject(ResultCode.SUCCESS.getCode(), data, message);
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     */
    public static ResultObject failed(IErrorCode errorCode) {
	  return new ResultObject(errorCode.getCode(), null, errorCode.getMessage());
    }

    /**
     * 失败返回结果
     *
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static ResultObject failed(IErrorCode errorCode, String message) {
	  return new ResultObject(errorCode.getCode(), null, message);
    }

    /**
     * @param errorMessage 错误信息
     * @Description://
     * @Return:
     * @Author: wzw
     * @Date: 2019/11/25 18:09
     */
    public static ResultObject errorReturn(String errorMessage) {
        return new ResultObject(500, errorMessage);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static ResultObject failed(String message) {
	  return new ResultObject(ResultCode.FAILED.getCode(), null, message);
    }

    /**
     * 失败返回结果
     */
    public static ResultObject failed() {
	  return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static ResultObject validateFailed() {
	  return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    public static ResultObject validateFailed(String message) {
	  return new ResultObject(ResultCode.VALIDATE_FAILED.getCode(), null, message);
    }

    /**
     * 未登录返回结果
     */
    public static <T> ResultObject unauthorized(T data) {
	  return new ResultObject(ResultCode.UNAUTHORIZED.getCode(), data,
					     ResultCode.UNAUTHORIZED.getMessage());
    }

    /**
     * 未授权返回结果
     */
    public static <T> ResultObject forbidden(T data) {
	  return new ResultObject(ResultCode.FORBIDDEN.getCode(), data,
					     ResultCode.FORBIDDEN.getMessage());
    }

    /**
     * code:200,500 Message:错误返回错误信息 data:正确返回对象
     */
    public static ResultObject successReturn(Object o) {
        return new ResultObject(200, o);
    }

    public static ResultObject resultReturn(com.cw.common.constant.Response o)
    {
        return new ResultObject(o.getCode(),o.getMessage());
    }
}
